const comp = (s1, s2) => {
  if (!(s1 instanceof Array)) {
    s1 = s1.split("");
  }
  if (!(s2 instanceof Array)) {
    s2 = s2.split("");
  }
  const curS2 = Array.from(s2);
  curS2.sort();
  return curS2.join("") === s1.join("");
};

var findAnagrams = function (s, p) {
  let L = 0;
  let R = 0;
  let len = s.length;
  let cur = [];
  let result = [];
  for (let i = 0; i < len; i++) {
    R++;
    cur.push(s[i]);
    if (R - L === p.length) {
      console.log(cur);
      if (comp(p, cur)) {
        console.log(L, R);
        result.push(L);
      }
      cur.shift();
      console.log("shift", cur);
      L++;
    }
  }
  return result;
};

console.log(findAnagrams("cbaebabacd", "abc"));
